/**
 * Copyright (c) 2013-2022 Nikita Koksharov
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.config;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 配置类，设置主从服务器模式下的Redission客户端配置
 *
 * @author Nikita Koksharov
 */
public class MasterSlaveServersConfig extends BaseMasterSlaveServersConfig<MasterSlaveServersConfig> {
    /**
     *  通过 Redisson MasterSlaveServersConfig，您可以实现以下功能：
     *     1、主从服务器连接配置，可以配置主服务器和从服务器的连接信息，包括主机名、端口号等。这使您能够指定 Redisson 客户端应该连接到哪些 Redis 服务器。
     *     2、读取策略设置：Redisson MasterSlaveServersConfig 提供了多种读取策略，如读取备份数据、读取主服务器数据等。您可以根据您的需求选择适当的读取策略，以确保数据的一致性和可用性。
     *     3、故障转移设置：如果您希望在主服务器故障时自动切换到从服务器，您可以在 Redisson MasterSlaveServersConfig 中启用故障转移功能，并设置故障转移延迟时间等参数
     *     4、负载均衡设置：如果您希望在多个从服务器之间分配负载，您可以在 Redisson MasterSlaveServersConfig 中启用负载均衡功能，并选择适当的负载均衡策略，如随机、轮询等
     */

    /**
     * 从节点地址
     */
    private Set<String> slaveAddresses = new HashSet<String>();

    /**
     * 主节点地址
     */
    private String masterAddress;

    /**
     * 选中的库名
     */
    private int database = 0;

    public MasterSlaveServersConfig() {
    }

    MasterSlaveServersConfig(MasterSlaveServersConfig config) {
        super(config);
        setLoadBalancer(config.getLoadBalancer());
        setMasterAddress(config.getMasterAddress());
        setSlaveAddresses(config.getSlaveAddresses());
        setDatabase(config.getDatabase());
    }


    public MasterSlaveServersConfig setMasterAddress(String masterAddress) {
        this.masterAddress = masterAddress;
        return this;
    }
    public String getMasterAddress() {
        return masterAddress;
    }


    public MasterSlaveServersConfig addSlaveAddress(String... addresses) {
        slaveAddresses.addAll(Arrays.asList(addresses));
        return this;
    }
    public MasterSlaveServersConfig addSlaveAddress(String slaveAddress) {
        slaveAddresses.add(slaveAddress);
        return this;
    }

    public Set<String> getSlaveAddresses() {
        return slaveAddresses;
    }

    public void setSlaveAddresses(Set<String> readAddresses) {
        this.slaveAddresses = readAddresses;
    }

    public MasterSlaveServersConfig setDatabase(int database) {
        this.database = database;
        return this;
    }

    public int getDatabase() {
        return database;
    }

}
